﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Xceed.Silverlight.Compatibility;
using Xceed.Utils.Licensing;

namespace Xceed.Silverlight.Controls
{
  public class FieldNameList : IList, INotifyCollectionChanged
  {
    public bool IsFixedSize
    {
      get
      {
        return ( m_values != null );
      }
    }

    public bool IsReadOnly
    {
      get
      {
        return ( m_values != null );
      }
    }

    public string this[ int index ]
    {
      get
      {
        return m_values[ index ];
      }
      set
      {
        if( DesignerProperties.GetIsInDesignMode() && ( index == this.Count ) )
        {
          this.Add( value );
          return;
        }

        m_values[ index ] = value;
      }
    }

    public int Count
    {
      get
      {
        return m_values.Count;
      }
    }

    public object SyncRoot
    {
      get
      {
        return this;
      }
    }

    public bool IsSynchronized
    {
      get
      {
        return false;
      }
    }

    public int Add( string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      if( this.Contains( value ) )
        throw new ArgumentException( "Value is already present in the list." );

      return ( ( IList )m_values ).Add( value );
    }

    public void Clear()
    {
      m_values.Clear();
    }

    public bool Contains( string value )
    {
      return m_values.Contains( value );
    }

    public int IndexOf( string value )
    {
      return m_values.IndexOf( value );
    }

    public void Insert( int index, string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      if( this.Contains( value ) )
        throw new ArgumentException( "Value is already present in the list." );

      m_values.Insert( index, value );
    }

    public void Remove( string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      m_values.Remove( value );
    }

    public void RemoveAt( int index )
    {
      m_values.RemoveAt( index );
    }

    public IEnumerator GetEnumerator()
    {
      return m_values.GetEnumerator();
    }

    public void CopyTo( Array array, int index )
    {
      ( ( ICollection )m_values ).CopyTo( array, index );
    }

    private ObservableCollection<string> m_values = new ObservableCollection<string>();

    #region INotifyCollectionChanged Implementation

    public event NotifyCollectionChangedEventHandler CollectionChanged
    {
      add
      {
        m_values.CollectionChanged += value;
      }
      remove
      {
        m_values.CollectionChanged -= value;
      }
    }

    #endregion

    #region IList Implementation

    int IList.Add( object value )
    {
      return this.Add( value as string );
    }

    void IList.Clear()
    {
      this.Clear();
    }

    bool IList.Contains( object value )
    {
      return this.Contains( value as string );
    }

    int IList.IndexOf( object value )
    {
      return this.IndexOf( value as string );
    }

    void IList.Insert( int index, object value )
    {
      this.Insert( index, value as string );
    }

    bool IList.IsFixedSize
    {
      get
      {
        return this.IsFixedSize;
      }
    }

    bool IList.IsReadOnly
    {
      get
      {
        return this.IsReadOnly;
      }
    }

    void IList.Remove( object value )
    {
      this.Remove( value as string );
    }

    void IList.RemoveAt( int index )
    {
      this.RemoveAt( index );
    }

    object IList.this[ int index ]
    {
      get
      {
        return this[ index ];
      }
      set
      {
        this[ index ] = value as string;
      }
    }

    void ICollection.CopyTo( Array array, int index )
    {
      this.CopyTo( array, index );
    }

    int ICollection.Count
    {
      get
      {
        return this.Count;
      }
    }

    bool ICollection.IsSynchronized
    {
      get
      {
        return this.IsSynchronized;
      }
    }

    object ICollection.SyncRoot
    {
      get
      {
        return this.SyncRoot;
      }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return this.GetEnumerator();
    }

    #endregion
  }
}
